/**
 * 
 * 地图部分公共方法
 *
 */
class MapHandleUtils {

	/**
	 * 获取数据源统一格式id
	 */
	getDataSourcrId(oldName) {
		return (oldName || '') + "DataSourceId"
	}

	/**
	 * 获取图层统一格式id
	 */
	getLayerSourcrId(oldName) {
		return (oldName || '') + "DataLayerId"
	}

	/**
	 * 将视角移动至地图中心点 
	 */
	setMapFlyto(map, lnglat, _zoom) {
		if (!map || !lnglat) return
		map.flyTo({
			center: lnglat,
			zoom: _zoom || 12,
		})
	}

	/**
	 * 获取图层可见最大最小缩放级别
	 */
	setLayerVisibleZoom(map, layerId, min, max) {
		if (!map || !layerId) return
		map.setLayerZoomRange(layerId, min, max);
	}

	/**
	 * 删除数据源
	 */
	deleteDataSource(map, name) {
		if (!map || !name) return
		if (map.getSource(name)) {
			map.removeSource(name)
		}
	}

	/**
	 * 删除layer
	 */
	deleteMapLayer(map, name) {
		if (!map || !name) return
		if (map.getLayer(name)) {
			map.removeLayer(name)
		}
	}


	/**
	 * 将 a 图层移动至 B 图层至上，即切换图层显示顺序
	 */
	changeMapLayerLevel(map, a, b) {
		if (!map) return
		map.moveLayer(b, a)
	}

	/**
	 * 隐藏图层
	 */
	hideMapLayer(map, layerid, status) {
		map.setLayoutProperty(layerid, 'visibility', 'none');
	}

	/**
	 * 显示图层
	 */
	showMapLayer(map, layerid, status) {
		map.setLayoutProperty(layerid, 'visibility', 'visible');
	}


	/**  ************  部分获取线对象、点对象等固定格式的方法 *********************** */

	/**
	 * 获取基础对象集合Obj
	 */
	getFeatureCollectionObj(array) {
		return {
			'type': 'FeatureCollection',
			'features': array || []
		}
	}

	/**
	 * 获取点对象
	 * properties = { "rotate": 0,  "title": 'test' }
	 */
	getPointFeatureObj(tempLngLat, properties) {
		let result = {
			'type': 'Feature',
			'geometry': {
				'type': 'Point',
				'coordinates': tempLngLat || []
			},
			// "properties": {
			// 	"rotate": 0,
			// 	"title": 'test'
			// }
		}
		if (properties) {
			result.properties = {
				...properties
			}
		}
		return result
	}

	/**
	 * 获取线对象
	 */
	getLineFeatureObj(coordinates, properties) {
		let result = {
			"type": "Feature",
			"geometry": {
				"type": "LineString",
				"coordinates": coordinates || []
			},
			// "properties": {
			// 	"title": 'test'
			// }
		}
		if (properties) {
			result.properties = {
				...properties
			}
		}
		return result
	}

	/**
	 * 获取圆点标记图层对象（节点）
	 */
	getCirclePointLayerObj(layerId, sourceId, properties) {
		let result = {
			"type": "circle",
			'id': layerId,
			'source': sourceId,
			// "paint": {
			// 	"circle-radius": 5,
			// 	"circle-color": "#1E90FF"
			// }
		}
		if (properties) {
			result.paint = {
				...properties
			}
		}
		return result
	}

	/**
	 * 获取标记图层对象（如线路名称）
	 */
	getSymbolLayerObj(layerId, sourceId, properties, paint) {
		let result = {
			"type": "symbol",
			'id': layerId,
			'source': sourceId,
			'layout': {
				'text-rotate': ['get', 'rotate'],
				'text-field': ['get', 'title'],
				"text-size": 12
			},
			'properties': {
				'rotate': 0,
				'title': "test"
			}
		}
		if (properties) {
			result.properties = {
				...properties
			}
		}
		if (paint) {
			result.paint = {
				...paint
			}
		}
		return result
	}
	getSymbolLayerForObj(layerId, sourceId, obj) {
		let result = {
			"type": "symbol",
			'id': layerId,
			'source': sourceId,
			'layout': {
				'text-rotate': ['get', 'rotate'],
				'text-field': ['get', 'title'],
				"text-size": 12
			},
			'properties': {
				'rotate': 0,
				'title': "test"
			}
		}
		if (obj.layout) {
			result.layout = {
				...result.layout,
				...obj.layout
			}
		}
		if (obj.properties) {
			result.properties = {
				...result.properties,
				...obj.properties
			}
		}
		if (obj.paint) {
			result.paint = {
				...result.paint,
				...obj.paint
			}
		}
		return result
	}

	/**
	 * 获取线图层对象（如管线）
	 */
	getLineLayerObj(layerId, sourceId, paint) {
		let result = {
			'type': 'line',
			'id': layerId,
			'source': sourceId,
			'layout': {
				'line-join': 'round',
				'line-cap': 'round'

			},
			'paint': {
				"line-color": "#EEE9E9",
				'line-width': 3
			}
		}
		if (paint) {
			result.paint = {
				...paint
			}
		}
		return result
	}
}

export default new MapHandleUtils();
